<?php

class SiteController extends Controller {
	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array (
				// captcha action renders the CAPTCHA image displayed on the
				// contact page
				'captcha' => array (
						'class' => 'CCaptchaAction',
						'backColor' => 0xFFFFFF 
				),
				// page action renders "static" pages stored under
				// 'protected/views/site/pages'
				// They can be accessed via: index.php?r=site/page&view=FileName
				'page' => array (
						'class' => 'CViewAction' 
				) 
		);
	}
	
	/**
	 * 检查服务器环境变量
	 */
	public function actionIndex() {
		// 高亮
		$this->channel = "system_info";
		$data = array ();
		$env = array (
				'system' => array (
						array (
								'操作系统',
								php_uname () 
						),
						array (
								'LANGUAGE',
								$_SERVER ['HTTP_ACCEPT_LANGUAGE'] 
						) 
				),
				'PHP' => array (
						array (
								'PHP版本',
								phpversion () 
						),
						array (
								'RUN_MODE',
								php_sapi_name () 
						),
						array (
								'$_SERVER状态',
								$this->checkServerVar () 
						),
						array (
								'PDO支持',
								extension_loaded ( 'PDO' ) 
						),
						array (
								'GD支持',
								$this->checkGD () 
						) 
				),
				'WEB' => array (
						array (
								'PORT',
								$_SERVER ['SERVER_PORT'] 
						),
						array(
							'ServerInfo',
							$this->getServerInfo()		
						)
				) 
		);
		$data ['env'] = $env;
		$this->render ( 'index', $data );
	}
	
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError() {
		if ($error = Yii::app ()->errorHandler->error) {
			if (Yii::app ()->request->isAjaxRequest)
				echo $error ['message'];
			else
				$this->render ( 'error', $error );
		}
	}
	
	/**
	 * Displays the contact page
	 */
	public function actionContact() {
		$model = new ContactForm ();
		if (isset ( $_POST ['ContactForm'] )) {
			$model->attributes = $_POST ['ContactForm'];
			if ($model->validate ()) {
				$headers = "From: {$model->email}\r\nReply-To: {$model->email}";
				mail ( Yii::app ()->params ['adminEmail'], $model->subject, $model->body, $headers );
				Yii::app ()->user->setFlash ( 'contact', 'Thank you for contacting us. We will respond to you as soon as possible.' );
				$this->refresh ();
			}
		}
		$this->render ( 'contact', array (
				'model' => $model 
		) );
	}
	
	/**
	 * Displays the login page
	 */
	public function actionLogin() {
		$this->layout = false;
		$model = new LoginForm ();
		
		// if it is ajax validation request
		if (isset ( $_POST ['ajax'] ) && $_POST ['ajax'] === 'login-form') {
			echo CActiveForm::validate ( $model );
			Yii::app ()->end ();
		}
		
		// collect user input data
		if (isset ( $_POST ['LoginForm'] )) {
			$model->attributes = $_POST ['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if ($model->validate () && $model->login ())
				$this->redirect ( Yii::app ()->user->returnUrl );
		}
		// display the login form
		$this->render ( 'login', array (
				'model' => $model 
		) );
	}
	
	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout() {
		Yii::app ()->user->logout ();
		$this->redirect ( Yii::app ()->homeUrl );
	}
	
	public function checkServerVar() {
		$vars = array (
				'HTTP_HOST',
				'SERVER_NAME',
				'SERVER_PORT',
				'SCRIPT_NAME',
				'SCRIPT_FILENAME',
				'PHP_SELF',
				'HTTP_ACCEPT',
				'HTTP_USER_AGENT' 
		);
		$missing = array ();
		foreach ( $vars as $var ) {
			if (! isset ( $_SERVER [$var] ))
				$missing [] = $var;
		}
		if (! empty ( $missing ))
			return '$_SERVER does not have {vars}.';
		
		if (realpath ( $_SERVER ["SCRIPT_FILENAME"] ) !== realpath ( __FILE__ ))
			return '$_SERVER["SCRIPT_FILENAME"] must be the same as the entry script file path.';
		
		if (! isset ( $_SERVER ["REQUEST_URI"] ) && isset ( $_SERVER ["QUERY_STRING"] ))
			return 'Either $_SERVER["REQUEST_URI"] or $_SERVER["QUERY_STRING"] must exist.';
		
		if (! isset ( $_SERVER ["PATH_INFO"] ) && strpos ( $_SERVER ["PHP_SELF"], $_SERVER ["SCRIPT_NAME"] ) !== 0)
			return 'Unable to determine URL path info. Please make sure $_SERVER["PATH_INFO"] (or $_SERVER["PHP_SELF"] and $_SERVER["SCRIPT_NAME"]) contains proper value.';
		
		return '';
	}
	
	//判断GD库
	public function checkGD() {
		if (extension_loaded ( 'gd' )) {
			$gdinfo = gd_info ();
			if ($gdinfo ['FreeType Support'])
				return 'INSTALLED WITH FreeType Support';
			return 'GD installed<br />FreeType support not installed';
		}
		return 'GD not installed';
	}
	
	//get serverinfo
	public function getServerInfo()
	{
		$info[]=isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : '';
		$info[]='<a href="http://www.yiiframework.com/">Yii Framework</a>/'.Yii::getVersion();
		$info[]=@strftime('%Y-%m-%d %H:%M',time());
		
		return implode(' ',$info);
	}
}